package com.example.yskjproject.base;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewbinding.ViewBinding;

import com.example.yskjproject.App;
import com.example.yskjproject.R;
import com.example.yskjproject.databinding.BaseActivityBinding;
import com.example.yskjproject.helper.interfaces.ActivityCallback;
import com.example.yskjproject.service.BottomDiscService;
import com.example.yskjproject.utils.EventBusUtil;

/**
 * 抽出来基类
 */
public abstract class BaseActivity<T extends ViewBinding> extends AppCompatActivity {
    public T mBinding;
    private BaseActivityBinding mBaseActivityBinding;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBaseActivityBinding = BaseActivityBinding.inflate(getLayoutInflater());
        setContentView(mBaseActivityBinding.getRoot());

        // 初始化 ViewBinding
        if (shouldSetBackground()) {
            mBaseActivityBinding.llRoot.setBackgroundResource(R.drawable.gradient_bg);
        }

        // 初始化 Toolbar
        setupToolbar();
        mBinding = getViewBinding();
        if (mBinding != null) {
            View rootView = mBinding.getRoot();
            Log.d("BaseActivity", "mBinding is initialized: " + (rootView != null));
            mBaseActivityBinding.contentFrame.addView(rootView);
        } else {
            Log.e("BaseActivity", "Failed to initialize content: mBinding or contentFrame is null");
        }

        // 注册 EventBus
        if (isRegisterEventBus()) {
            EventBusUtil.register(this);
        }

        initData();
    }


    protected boolean shouldSetBackground() {
        return true; // 默认设置背景
    }

    /**
     * 子类实现 ViewBinding
     */
    protected abstract T getViewBinding();

    /**
     * 子类初始化数据
     */
    protected abstract void initData();

    /**
     * 是否注册事件分发
     * 默认不注册，子类可复写返回 true。
     */
    protected boolean isRegisterEventBus() {
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        // 反注册 EventBus
        if (isRegisterEventBus()) {
            EventBusUtil.unregister(this);
        }
//        Intent stopIntent = new Intent(this, BottomDiscService.class);
//        stopService(stopIntent);
//        seriaPortHelper.closeSerial();
        mBinding = null;
    }

    private long backPressedTime;

    @SuppressLint("MissingSuperCall")
    @Override
    public void onBackPressed() {
        finish();
    }

    private ActivityCallback callback;

    protected void someMethodWhereYouWantToCallSubClassMethod() {
        if (callback != null) {
            callback.doSomethingInActivity();
//            settingMode = "mode5";
//            zeroInt = 0;
//            seriaPortHelper.openSerial(false, peopleBoolean, settingMode, zeroInt, mBaseActivityBinding.lcBottomDisc.disKg.getText().toString().trim(), 0);
        }
    }

    public void setActivityCallback(ActivityCallback callback) {
        this.callback = callback;
    }

    @Override
    protected void onPause() {
        super.onPause();
//        if (((App) getApplication()).isAppInBackground()) {
//            Intent stopIntent = new Intent(this, BottomDiscService.class);
//            stopService(stopIntent);
//        }
    }

    @Override
    protected void onResume() {
        super.onResume();
//        if (((App) getApplication()).isAppInBackground()) {
//            Intent startService = new Intent(this, BottomDiscService.class);
//            startService(startService);
//        }

    }

    /**
     * 初始化 Toolbar
     */
    protected void setupToolbar() {
        setSupportActionBar(mBaseActivityBinding.toolbar);

        mBaseActivityBinding.leftButton.setOnClickListener(v -> onBackPressed());

        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayShowTitleEnabled(false); // 禁用默认标题
        }

        // 根据子类设置决定标题是否可见
        if (shouldShowTitle()) {
            mBaseActivityBinding.toolbar.setVisibility(View.VISIBLE);
        } else {
            mBaseActivityBinding.toolbar.setVisibility(View.GONE);
        }
    }

    /**
     * 是否显示标题栏标题
     * 子类可重写该方法，根据需求返回 false 隐藏标题。
     */
    protected boolean shouldShowTitle() {
        return true; // 默认显示标题
    }


    /**
     * 设置标题
     */
    protected void setTitle(String title) {
        mBaseActivityBinding.toolbarTitle.setText(title);
    }


    /**
     * 设置右侧按钮
     */
    protected void setupRightButton(String buttonText, View.OnClickListener onClickListener) {
        TextView rightButton = new TextView(this);
        rightButton.setText(buttonText);
        rightButton.setOnClickListener(onClickListener);
        mBaseActivityBinding.toolbar.addView(rightButton); // 动态添加右侧按钮
    }
}
